package com.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.R;
import com.edu.eduservice.domain.EduTeacher;
import com.edu.eduservice.domain.QueryTeacher;
import com.edu.eduservice.mapper.EduTeacherMapper;
import com.edu.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-12
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    //自动注入
    @Autowired(required = false)
    private EduTeacherMapper eduTeacherMapper;


    @Override
    public R pagwShowService(QueryTeacher teacherQuery, Integer number, Integer pageSize) {
        //条件
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper();
        //添加条件
        if(teacherQuery != null) {
            if(!StringUtils.isEmpty(teacherQuery.getSearchName()))
                wrapper.like("name", teacherQuery.getSearchName());
            if(teacherQuery.getSearchLevel()!=null && teacherQuery.getSearchLevel()!="")
                wrapper.eq("level", teacherQuery.getSearchLevel());
        }
        wrapper.orderByDesc("gmt_create");
        //分页
        IPage<EduTeacher> page=new Page<>(number,pageSize);
         page = eduTeacherMapper.selectPage(page, wrapper);

         //返回
        R r = R.ok();
        r.data("total", page.getTotal());
        r.data("row",page.getRecords());
        return r;
    }

    @Override
    public Map<String, Object> frontgetPage(Page<EduTeacher> page) {
        //分页并且根据id降序排列
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("id");
        baseMapper.selectPage(page,wrapper);
        //把分页的数据存放到map集合
        Map<String,Object> map=new HashMap<>();

        map.put("items", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());

        return map;
    }
}
